package com.tinckay.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tinckay.common.util.ShiroUtils;
import com.tinckay.entity.po.SysLog;
import com.tinckay.service.SysLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.hutool.core.util.StrUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Jean
 * @since 2018-10-21
 */
@Api(tags = "日志管理")
@RestController
@RequestMapping("/sysLog")
public class SysLogController extends ApiController {

    @Autowired
    private SysLogService sysLogService;

    @GetMapping("/list")
    @ApiOperation("分页查询日志列表")
    //@RequiresPermissions("sysLog:list")
    public R<IPage> list(@RequestParam(required = false) String username,
                         @RequestParam(required = false) Integer logType,
                         @RequestParam(defaultValue = "1") int current,
                         @RequestParam(defaultValue = "10") int size) {

        LambdaQueryWrapper<SysLog> wrapper = new QueryWrapper<SysLog>()
                .lambda()
                .select(SysLog::getId,
                        SysLog::getUserId,
                        SysLog::getUsername,
                        SysLog::getOperation,
                        SysLog::getTime,
                        SysLog::getMethod,
                        SysLog::getIp,
                        SysLog::getLocation,
                        SysLog::getLogType,
                        SysLog::getDataTime
                )
                .eq(logType != null, SysLog::getLogType, logType)
                .eq(!ShiroUtils.isAdmin(), SysLog::getUserId, ShiroUtils.getCurrentUserId())
                .like(StrUtil.isNotBlank(username) && ShiroUtils.isAdmin(), SysLog::getUsername, username)
                .orderByDesc(SysLog::getDataTime);

        IPage<SysLog> page = sysLogService.page(new Page<>(current, size), wrapper);
        return success(page);
    }

}

